/*
 * @Date: 2025-06-13 11:12:23
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-09-04 15:38:41
 * @FilePath: \corpus\src\pages\Foundation\Manage\Cornerstone\index.tsx
 */
import type { ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import hooks from '../../hooks/useHooks';

import { getBaseList } from '@/services/ant-design-pro/foundation';
import { Card, Form, Space, Tag } from 'antd';
import React, { useState } from 'react';
type typeCount = {
  name: string;
  countNumber: number;
};

const Cornerstone: React.FC = () => {
  const { name, actionRef, data, levelData, setSelectedKeys, id } = hooks.useContainer();
  const [form] = Form.useForm();

  const [count, setCount] = useState(0);

  const [selectedTags, setSelectedTags] = useState<typeCount[]>([]);
  const [selectedTagsType, setSelectedTagsType] = useState<typeCount[]>([]);

  const columns: ProColumns<any>[] = [
    {
      dataIndex: 'index',
      valueType: 'indexBorder',
      width: 48,
      title: '序号',
    },

    {
      title: '原始文件名称',
      dataIndex: 'fileName',
      ellipsis: true,
    },

    {
      title: '是否文本',
      dataIndex: 'contentQuery',
      initialValue: '0',
      hideInTable: true,
      width: 100,
      valueType: 'radio',
      valueEnum: {
        1: { text: '是', status: 'yes' },
        0: { text: '否', status: 'no' },
      },
    },

    {
      title: '语料内容',
      dataIndex: 'fileSplitContent',
      ellipsis: true,
      hideInSearch: true,
    },
    {
      title: '章',
      dataIndex: 'fileSplitChapter',
      ellipsis: true,
      hideInSearch: true,
    },
    {
      title: '条',
      dataIndex: 'fileSplitClause',
      ellipsis: true,
      hideInSearch: true,
    },
    {
      title: '发文部门',
      dataIndex: 'sourceOrg',
      ellipsis: true,
      hideInSearch: true,
    },
    {
      title: '发文字号',
      dataIndex: 'gzwh',
      ellipsis: true,
      hideInSearch: true,
    },
    {
      title: '公布日期',
      dataIndex: 'fbrq',
      ellipsis: true,
      hideInSearch: true,
    },
    {
      title: '施行日期',
      dataIndex: 'sxrq',
      ellipsis: true,
      hideInSearch: true,
    },
  ];
  const handleChange = (tag: typeCount, checked: boolean) => {
    const nextSelectedTags = checked
      ? [...selectedTags, tag]
      : selectedTags.filter((t) => t !== tag);
    setSelectedTags(nextSelectedTags);
    const arr = nextSelectedTags?.map((item: typeCount) => item?.name);
    form.setFieldValue('name', arr);
    console.log(form.getFieldValue('name'));
  };
  const handleChangeType = (tag: typeCount, checked: boolean) => {
    const nextSelectedTags = checked
      ? [...selectedTagsType, tag]
      : selectedTagsType.filter((t) => t !== tag);
    setSelectedTagsType(nextSelectedTags);
    const arr = nextSelectedTags?.map((item: typeCount) => item?.name);
    form.setFieldValue('type', arr);
    console.log(form.getFieldValue('type'));
  };

  return (
    <ProTable<API.RuleListItem, API.PageParams>
      toolBarRender={() => []}
      rowKey="key"
      actionRef={actionRef}
      headerTitle={
        <Space align="center">
          <span style={{ fontSize: '14.0px' }}>基础语料库总数：{count}条</span>
        </Space>
      }
      request={async (
        // 第一个参数 params 查询表单和 params 参数的结合
        // 第一个参数中一定会有 pageSize 和  current ，这两个参数是 antd 的规范
        params: any,
      ) => {
        // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
        // 如果需要转化参数可以在这里进行修改
        if (params.contentQuery === '0') {
          params.contentQuery = false;
        } else {
          params.contentQuery = true;
        }
        const msg = await getBaseList({
          ...params,
          sourceOrg: name,
          // fileType1: form.getFieldValue('name'),
          knowledgeId: id,
        });
        setCount(msg.total);
        return {
          data: msg.data,
          // success 请返回 true，
          // 不然 table 会停止解析数据，即使有数据
          success: true,
          // 不传会使用 data 的长度，如果是分页一定要传
          total: msg.total,
        };
      }}
      onReset={() => {
        setSelectedKeys([]);
        actionRef?.current?.reset();
      }}
      tableExtraRender={(_, dataBak) => {
        return (
          <Card bodyStyle={{ padding: '10.0px' }}>
            <div style={{ marginLeft: '10.0px' }}>
              <Form labelCol={{ span: 1 }} wrapperCol={{ span: 24 }} form={form}>
                <Form.Item label="分级" name="name">
                  {levelData.map((tag) => (
                    <Tag.CheckableTag
                      key={tag.name}
                      checked={selectedTags.indexOf(tag) > -1}
                      onChange={(checked) => handleChange(tag, checked)}
                    >
                      {tag.name} ({tag.countNumber}个)
                    </Tag.CheckableTag>
                  ))}
                </Form.Item>

                <Form.Item label="分类" name="password">
                  {data.map((tag) => (
                    <Tag.CheckableTag
                      key={tag.name}
                      checked={selectedTagsType.indexOf(tag) > -1}
                      onChange={(checked) => handleChangeType(tag, checked)}
                    >
                      {tag.name} ({tag.countNumber}个)
                    </Tag.CheckableTag>
                  ))}
                </Form.Item>
              </Form>
            </div>
          </Card>
        );
      }}
      search={{
        labelWidth: 'auto',
      }}
      pagination={{
        showSizeChanger: true,
        defaultPageSize: 10,
      }}
      columns={columns}
    />
  );
};

export default Cornerstone;
